package com.example.springboot_jpa.service;


import com.example.springboot_jpa.dao.EmployeeRepository;
import com.example.springboot_jpa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class EmployeeServiceImpl implements EmployeeService{
    private final EmployeeRepository employeeRepository;
    @Autowired
    public EmployeeServiceImpl(EmployeeRepository employeeRepository) {

        this.employeeRepository = employeeRepository;
    }

    @Override
    public List<Employee> getAllEmployees() {
        return employeeRepository.findAll();
    }

    @Override
    public void saveEmployee(Employee employee) {
        employeeRepository.save(employee);
    }

    @Override
    public Employee getEmployee(int id) {
        Employee employee=null;
       Optional<Employee>optional=employeeRepository.findById(id);
       if (optional.isPresent()){
           employee=optional.get();
       }
       else {
           throw new NullPointerException();
       }
       return employee;
    }

    @Override
    public void deleteEmployee(int id) {
        employeeRepository.deleteById(id);
    }

    public List<Employee> getEmpBySalary(int salary){
       return employeeRepository.findAllBySalaryGreaterThan(salary);
    }
}
